package com.ycyun.admin.utils;

import com.ycyun.admin.dto.LoginUser;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @Author: 哦谢特谢特
 * @Date: 2018/5/21 19:50
 * @Description: 获取登录用户
 */
public class UserUtil {

    public static LoginUser getLoginUser() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null) {
            if (authentication instanceof AnonymousAuthenticationToken){
                return null;
            }

            if (authentication instanceof UsernamePasswordAuthenticationToken) {
                return (LoginUser)authentication.getPrincipal();
            }
        }

        return null;


    }


}
